package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * https://leetcode.cn/problems/partition-labels/description/?envType=study-plan-v2&envId=top-100-liked
 * 哈希表+贪心
 * 自我AC
 * 优化：哈希表的形式可以更改为数组，因为只包含小写字母
 *
 * @author yulongTian
 * @create 2024-07-20 10:16
 */
public class Test76 {
    public static void main(String[] args) {

    }

    public List<Integer> partitionLabels(String s) {
        List<Integer> res = new ArrayList<>();
        HashMap<Character, Integer> map = new HashMap<>();
        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            char c = chs[i];
            map.put(c, i);
        }
        int pre_Index = 0;
        for (int i = 0; i < chs.length; i++) {
            int temp_part_boundry = map.get(chs[i]);
            for (int j = i + 1; j < temp_part_boundry; j++) {
                Integer temp_max_index = map.get(chs[j]);
                temp_part_boundry = Math.max(temp_part_boundry, temp_max_index);
            }
            pre_Index = i;
            i = temp_part_boundry;
            res.add(temp_part_boundry + 1 - pre_Index);
        }
        return res;
    }


}
